﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using HouseHoldBudget.Core.Entities.Expenses;
using HouseHoldBudget.Models.Expenses;
using HouseHoldBudget.Services.Expenses;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;

#endregion

namespace HouseHoldBudget.Controllers
{
    public class DashboardsController : BudgetControllerBase
    {
        #region Readonly & Static Fields

        private readonly IExpenseService _expenseService;
        private readonly IExpenseTypesService _expenseTypesService;

        #endregion

        #region Constructors

        public DashboardsController(IExpenseService expenseService,
            IExpenseTypesService expenseTypesService)
        {
            _expenseService = expenseService;
            _expenseTypesService = expenseTypesService;
        }

        #endregion

        #region Instance Methods

        /// <summary>
        ///   Gets the last expenses summary.
        /// </summary>
        /// <returns> </returns>
        public ActionResult GetLastExpensesSummary([DataSourceRequest]DataSourceRequest request)
        {
            var model = GetLastExpensesModel();
            DataSourceResult result = model.ToDataSourceResult(request);
            return Json(result);
        }

        /// <summary>
        ///   Lasts the expenses summary.
        /// </summary>
        /// <returns> </returns>
        [ChildActionOnly]
        public ActionResult LastExpensesSummary()
        {
            return PartialView(GetLastExpensesModel());
        }

        [ChildActionOnly]
        public ActionResult MyBudgetSummary()
        {
            return PartialView();
        }

        /// <summary>
        ///   Prepares the expense model for list.
        /// </summary>
        /// <param name="expense"> The expense. </param>
        /// <returns> </returns>
        [NonAction]
        protected ExpenseModel PrepareExpenseModelForList(Expense expense)
        {
            var model = new ExpenseModel
                       {
                           ExpenseAmount = expense.ExpenseAmount,
                           ExpenseLabel = expense.ExpenseLabel,
                           Id = expense.Id,
                           ExpenseOn = expense.ExpenseOn,
                       };

            //Get type
            var expenseTypeId = _expenseTypesService.GetExpenseTypeById(expense.ExpensesTypeId.Value);
            model.ExpenseType = new ExpenseTypesModel
            {
                Id = expenseTypeId.Id,
                ExpenseTypeName = expenseTypeId.ExpenseTypeName,
            };

            return model;
        }

        /// <summary>
        ///   Gets the last expenses model.
        /// </summary>
        /// <returns> </returns>
        [NonAction]
        private IList<ExpenseModel> GetLastExpensesModel()
        {
            var expenses = _expenseService.GetExpensesByUserId(CurrentBudgetUser.Id, null, DateTime.Now);
            expenses = expenses.Take(5).OrderByDescending(p=>p.ExpenseOn).ToList(); //todo: configure in user settings
            return expenses.Select(PrepareExpenseModelForList).ToList();
        }

        #endregion
    }
}